require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroManorAutoHandler ={}
local this = RedDotsHeroManorAutoHandler

function RedDotsHeroManorAutoHandler.init()
    CloseEvent(Config.EventType.FreshManorInfo,this.update);
    ListenEvent(Config.EventType.FreshManorInfo,this.update);
    return this
end

function RedDotsHeroManorAutoHandler.getFlag(...)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroManor,false) then
        local partnerId = ...;
        if partnerId ~= nil then
            if logicMgr.HeroManorManager.isManorMaster(partnerId) then
                local cityInd, index = logicMgr.HeroManorManager.getMasterCityIdAndIndex(partnerId);
                if logicMgr.HeroManorManager.isAutoRed(cityInd, index) then
                    return Config.RedDotsFlagState.Needful;
                end
            end
        else
            local data = logicMgr.HeroManorManager.getManorInfo(1);
            if data ~= nil then
                for k, v in pairs(data) do
                    if v.masterId ~= nil and tonumber(v.masterId) ~= 0 then
                        local cityInd, index = logicMgr.HeroManorManager.getMasterCityIdAndIndex(v.masterId);
                        if logicMgr.HeroManorManager.isAutoRed(cityInd, index) then
                            return Config.RedDotsFlagState.Needful;
                        end
                    end
                end
            end
        end
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsHeroManorAutoHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroManorAuto);
end